package com.yln.watcher.client;

import com.yln.watcher.AssignedTaskWatcher;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;

@Slf4j
public class RegisterWorkerCallback implements AsyncCallback.Create2Callback {
    @Override
    public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
        ZkClient client = (ZkClient) ctx;
        log.info("register worker: {} to {}", client.getClientId(), path);
        KeeperException.Code code = KeeperException.Code.get(rc);
        switch (code) {
            case OK:
                // 注册成功 监听/assign/{worker-id}下的children
                client.createAssignWorkerNodeAndWatch();
//                client.addWatcher(client.getAssignedNodePath(), new AssignedTaskWatcher(client));
                break;
            case NODEEXISTS:
                // worker重复 注册失败
                log.error("client conflict, client id: ", client.getClientId());
                client.stop();
                break;
            default:
                ;

        }
    }
}
